﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PolePosition.v3.EntityFactory
{
    public class AdministrationDataFactory
    {
        public static EntityLibrary.LoginEntity DoLogin(string userName, string password)
        {
            DataSet _Data;
            _Data = EntityFactory.StoredProceduresFactory.Login(userName, password);

            if (_Data == null || _Data.Tables.Count < 2 || _Data.Tables[0].Rows.Count != 1)
                return null;

            EntityLibrary.LoginEntity.LoginUserTypes _LoginType = EntityLibrary.LoginEntity.LoginUserTypes.Unknown;
            try
            {
                _LoginType = (EntityLibrary.LoginEntity.LoginUserTypes)Enum.Parse(typeof(EntityLibrary.LoginEntity.LoginUserTypes), _Data.Tables[0].Rows[0].Field<string>(0));
            }
            catch { }

            EntityLibrary.LoginEntity _LoginResult = new EntityLibrary.LoginEntity();
            _LoginResult.Success = true;
            _LoginResult.LoginUserType = _LoginType;

            switch (_LoginType)
            {
                case EntityLibrary.LoginEntity.LoginUserTypes.Unknown:
                    _LoginResult.Success = false;
                    return _LoginResult;
                case EntityLibrary.LoginEntity.LoginUserTypes.Vendor:

                    if (_Data.Tables[2].Rows.Count != 0)
                        _LoginResult.Vendor = new EntityLibrary.VendorEntity(_Data.Tables[2].Rows[0]);

                    if (_Data.Tables[3].Rows.Count != 0)
                        _LoginResult.VendorContact = new EntityLibrary.VendorContactEntity(_Data.Tables[3].Rows[0]);

                    if (_Data.Tables[4].Rows.Count != 0)
                        _LoginResult.Utilities = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityEntity>(_Data.Tables[4]);

                    if (_Data.Tables[5].Rows.Count != 0)
                        _LoginResult.UtilityContacts = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityContactEntity>(_Data.Tables[5]);

                    break;
                case EntityLibrary.LoginEntity.LoginUserTypes.Utility:

                    if (_Data.Tables[2].Rows.Count != 0)
                        _LoginResult.Utility = new EntityLibrary.UtilityEntity(_Data.Tables[2].Rows[0]);

                    if (_Data.Tables[3].Rows.Count != 0)
                        _LoginResult.UtilityContact = new EntityLibrary.UtilityContactEntity(_Data.Tables[3].Rows[0]);

                    if (_Data.Tables[4].Rows.Count != 0)
                        _LoginResult.Polygons = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityPolygonEntity>(_Data.Tables[4]);

                    if (_Data.Tables[5].Rows.Count != 0)
                        _LoginResult.Vendors = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorEntity>(_Data.Tables[5]);

                    if (_Data.Tables[6].Rows.Count != 0)
                        _LoginResult.VendorContacts = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorContactEntity>(_Data.Tables[6]);

                    break;
                case EntityLibrary.LoginEntity.LoginUserTypes.PPI:

                    if (_Data.Tables[2].Rows.Count != 0)
                        _LoginResult.Polygons = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityPolygonEntity>(_Data.Tables[2]);

                    if (_Data.Tables[3].Rows.Count != 0)
                        _LoginResult.Utilities = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityEntity>(_Data.Tables[3]);

                    if (_Data.Tables[4].Rows.Count != 0)
                        _LoginResult.UtilityContacts = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.UtilityContactEntity>(_Data.Tables[4]);

                    if (_Data.Tables[5].Rows.Count != 0)
                        _LoginResult.Vendors = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorEntity>(_Data.Tables[5]);

                    if (_Data.Tables[6].Rows.Count != 0)
                        _LoginResult.VendorContacts = new ProjectCommon2.Generics.EntityCollection<EntityLibrary.VendorContactEntity>(_Data.Tables[6]);

                    break;
                default:
                    return null;
            }

            _LoginResult.Projects = new ProjectCommon2.Generics.EntityCollection<PolePosition.v3.EntityLibrary.ProjectEntity>(_Data.Tables[1]);

            return _LoginResult;
        }


    }
}
